Imports System.Windows.Forms
Imports System.Data
'SMO namespaces
Imports Microsoft.SqlServer.Management.Common
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer
Imports SQLAnalyzer.Framework
Imports SQLAnalyzer.Modules

Public Class MainForm

    Private currentServer As Server
    Private currentServerInfo As ServerInfo

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim loginForm As Login
        Dim dr As DialogResult
        Dim serverConn As ServerConnection

        'Display the main window first
        Me.Show()
        serverConn = New ServerConnection()

        loginForm = New Login(ServerConn)
        loginForm.Icon = Me.Icon
        dr = loginForm.ShowDialog(Me)


        If (dr = DialogResult.OK) AndAlso (serverConn.SqlConnectionObject.State = ConnectionState.Open) Then
            currentServer = New Server(serverConn)
            If currentServer IsNot Nothing Then
                Dim childForm As IAnalyzerModule
                childForm = New TablesForm()
                childForm.Initialize(currentServer)
                childForm.MdiParent = Me
                childForm.Show()
            End If
            UpdateStatusBar("Ready", currentServer.Name)
        Else
            Me.Close()
        End If
    End Sub

    Private Sub UpdateStatusBar(ByVal statusMessage As String, Optional ByVal serverName As String = "Not Connected")
        lblStatusMessage.Text = statusMessage
        lblStatusServer.Text = serverName
    End Sub

    'Private m_ChildFormNumber As Integer
    'Private Sub ShowNewForm(ByVal sender As Object, ByVal e As EventArgs) Handles NewToolStripMenuItem.Click, NewToolStripButton.Click, NewWindowToolStripMenuItem.Click
    '    ' Create a new instance of the child form.
    '    Dim ChildForm As New System.Windows.Forms.Form
    '    ' Make it a child of this MDI form before showing it.
    '    ChildForm.MdiParent = Me

    '    m_ChildFormNumber += 1
    '    ChildForm.Text = "Window " & m_ChildFormNumber

    '    ChildForm.Show()
    'End Sub

    Private Sub ExitToolsStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub ToolBarToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ToolBarToolStripMenuItem.Click
        Me.ToolStrip.Visible = Me.ToolBarToolStripMenuItem.Checked
    End Sub

    Private Sub StatusBarToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles StatusBarToolStripMenuItem.Click
        Me.StatusStrip.Visible = Me.StatusBarToolStripMenuItem.Checked
    End Sub

    Private Sub CascadeToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CascadeToolStripMenuItem.Click
        Me.LayoutMdi(MdiLayout.Cascade)
    End Sub

    Private Sub TileVerticleToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles TileVerticalToolStripMenuItem.Click
        Me.LayoutMdi(MdiLayout.TileVertical)
    End Sub

    Private Sub TileHorizontalToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles TileHorizontalToolStripMenuItem.Click
        Me.LayoutMdi(MdiLayout.TileHorizontal)
    End Sub

    Private Sub ArrangeIconsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ArrangeIconsToolStripMenuItem.Click
        Me.LayoutMdi(MdiLayout.ArrangeIcons)
    End Sub

    Private Sub CloseAllToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CloseAllToolStripMenuItem.Click
        ' Close all child forms of the parent.
        For Each ChildForm As Form In Me.MdiChildren
            ChildForm.Close()
        Next
    End Sub
End Class
